<?php
/**
 * Application bootstrap
 * 
 * @uses    Zend_Application_Bootstrap_Bootstrap
 * @package FireFeed
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

	private $_acl = null;
	private $_auth = null;
	
	/**
     * Bootstrap autoloader for application resources
     * 
     * @return Zend_Application_Module_Autoloader
     */
    protected function _initAutoload()
    {
        $moduleloader = new Zend_Application_Module_Autoloader(array(
            			'namespace' => '',
            			'basePath'  => dirname(__FILE__),
        ));
        
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace(array('FireFeed_'));
        
        return $moduleloader;
    }
    
    /**
     * Init Zend_Acl module for user access control
     * and set front controller plugin for acl check
     * 
     * @return void
     */
    protected function _initAcl()
    {
    	$this->_acl = new FireFeed_Acl_Acl();
        $this->_auth = Zend_Auth::getInstance();
        
        $fc = Zend_Controller_Front::getInstance();
        $fc->registerPlugin(new FireFeed_Plugin_Acl($this->_acl, $this->_auth));
    }
    
    /**
     * Set the concrete instance for FireFeed model factory
     * 
     * @return void
     */
    protected function _initFactory()
    {
    	Model_Factory::setInstance(new Model_ZendDb_Factory());
    }
    
    /**
     * Instantiate Zend_Locale into Zend_Registry for global 
     * application localization.
     * 
     * @return void
     */
    protected function _initLocale()
    {
		// default language when requested language is not available
		$defaultlanguage = 'en';
		
		// get logged in user's language 
        if ($this->_auth->hasIdentity()) {
		  $language = $this->_auth->getIdentity()->getUserProfile()->getLanguage();  
        } else { // or fallback to browser language
          $language = Zend_Locale::BROWSER;
        }
        
		$translate = new Zend_Translate(
		    Zend_Translate::AN_CSV, 
            LOCALE_PATH, 
            'auto', 
            array('scan' => Zend_Translate::LOCALE_DIRECTORY)
        );
        
        // try tu set user languages as default
        try {
		  $locale = new Zend_Locale($language);
        } catch (Zend_Locale_Exception $e) {
          // On errors fallback to BROWSER locale
          $locale = new Zend_Locale(Zend_Locale::BROWSER);
        }
		
        // If not avalable previus selected locale, set english one as default
		if (!$translate->isAvailable($locale->getLanguage())) {
		    $translate->setLocale($defaultlanguage);
		} else {
			$translate->setLocale($locale->getLanguage());
		}
		
		Zend_Registry::set('Zend_Translate', $translate);
    }
    
    /**
     * Default inclusion of CSS and Javascript
     * 
     * @return Zend_View
     */
    protected function _initHeadInclusion()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_TRANSITIONAL');
		
		$view->headLink()->appendStylesheet('/css/style.css');
        $view->headLink()->appendStylesheet('/css/TextStyle.css');
        $view->headLink()->appendStylesheet('/css/inettuts.css');
        $view->headLink()->appendStylesheet('/css/inettuts.js.css');
        $view->headLink()->appendStylesheet('/css/slide.css');
        $view->headLink()->appendStylesheet('/css/facebox.css');
        $view->headLink()->appendStylesheet('/css/menu.css');
        $view->headLink()->appendStylesheet('/css/jquery-ui-1.7.2.ui-lightness.css');
        $view->headLink()->appendStylesheet('/css/jquery.tip.css');

        $view->headScript()->appendFile('/js/jquery-1.3.2.min.js');
        $view->headScript()->appendFile('/js/jquery-ui-1.7.2.custom.min.js');
        $view->headScript()->appendFile('/js/facebox.js');
        $view->headScript()->appendFile('/js/utils.js');
        $view->headScript()->appendFile('/js/jquery.tip.js');
        
        
        return $view;
    }
    
    
    protected function _initNavigation() 
    {
    	$this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();
      	
        /*Navigation tramite file xml*/
        $config = new Zend_Config_Xml(
            APPLICATION_PATH . '/configs/navigation.xml', 'nav');
    	$navigation = new Zend_Navigation($config);
        
        if ($this->_auth->hasIdentity()) {
        	$role = $this->_auth->getStorage()->read()->roles->name;
        } else {
        	$role = FireFeed_Acl_Roles::GUEST;
        }
        
 		$view->navigation($navigation)->setAcl($this->_acl)->setRole($role); 
    }
    
}

